package cc.rico.tutorial.workqueue;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.stream.IntStream;

import cc.rico.tutorial.RabbitMQConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import lombok.extern.slf4j.Slf4j;


/**
 * @author rico 2017年6月26日
 *
 */
@Slf4j
public class NewTask {
	private static final String TASK_QUEUE_NAME = "task_queue";

	/**
	 * @param args
	 * @throws TimeoutException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn = RabbitMQConnectionUtil.getConnection();
		Channel channel = conn.createChannel();
		
		// queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
		channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
		
		String message = "Message From Task Queuer";
		IntStream.range(1, 10).forEach(e -> {
			try {
				String taskMessage = message + " [" + e + "]";
				log.info("taskMessage = {}", taskMessage);
				channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, taskMessage.getBytes());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		});

		//log.info("Sent.message = {}", message);
		RabbitMQConnectionUtil.close(conn, channel);
	}
}
